package com.example.demo.controller;

import com.example.demo.api.SmsApi;
import com.example.demo.dao.monDao.PersonResultDao;
import com.example.demo.dto.GrassAndCar;
import com.example.demo.dto.PhotoSpeak;
import com.example.demo.dto.monDTO.PersonResultMongo;
import com.example.demo.service.Tencent_VisionService;
import com.example.demo.utils.FileSaveLocalUtil;
import com.example.demo.utils.KeyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@RestController
@Api(tags = "计算机视觉")
public class VisionController {
    @Autowired
    private Tencent_VisionService tencent_visionService;

    @Autowired
    private PersonResultDao personResultDao;  //直接注入不写service了。
    @PostMapping("/getGrass_Car")
    @ApiOperation("花草和车辆识别")
    public String getGrass_Car(@ApiParam("提交需要识别的照片") @RequestParam("file")MultipartFile file,
                               @ApiParam("识别场景<br>1-车辆识别<br>2-花草识别")
                               @RequestParam("scene")String scene,
                               @ApiParam("用户openid,可提交可不提交，不提交默认undefined") @RequestParam(value = "openid",defaultValue = "undefined") String openid)throws Exception {
        String fileName = KeyUtil.genUniqueKey();
        FileSaveLocalUtil.savePic(file.getInputStream(), fileName);
        String result = tencent_visionService.getGrass_Car(file.getBytes(),scene);
        //对信息的保存处理
        if("1".equals(scene)||"2".equals(scene)) {
            JSONParser parser = new JSONParser();
            JSONObject data = (JSONObject) parser.parse(result);
            GrassAndCar persion_id = new GrassAndCar(data);
            PersonResultMongo personResultMongo = new PersonResultMongo();
            personResultMongo.setId(KeyUtil.genUniqueKey());
            personResultMongo.setImgUrl(SmsApi.YU_MING + "getPhoto?photoId=/" + fileName + ".jpg");    //输入的图片
            personResultMongo.setResult(persion_id); //结果
            personResultMongo.setCreateTime(new Date());  //创建日期
            personResultMongo.setOpenid(openid);  //用户openid
            if("1".equals(scene)){
                personResultMongo.setType("车辆识别");
            }else if("2".equals(scene)){
                personResultMongo.setType("花草识别");
            }
            personResultDao.save(personResultMongo);
        }
        return result;
    }
    @PostMapping("/getSpeakOnce")
    @ApiOperation("看图说一句话")
    public String getSpeakOnce(@ApiParam("提交需要识别的照片") @RequestParam("file")MultipartFile file,
                               @ApiParam("用户openid,可提交可不提交，不提交则不会保存") @RequestParam(value = "openid",defaultValue = "undefined") String openid)throws Exception {
        String fileName = KeyUtil.genUniqueKey();
        FileSaveLocalUtil.savePic(file.getInputStream(), fileName);
        String result = tencent_visionService.getSpeakOnce(file.getBytes());
        //对信息的保存处理
        JSONParser parser = new JSONParser();
        JSONObject data = (JSONObject) parser.parse(result);
        PhotoSpeak persion_id = new PhotoSpeak(data);
        PersonResultMongo personResultMongo = new PersonResultMongo();
        personResultMongo.setId(KeyUtil.genUniqueKey());
        personResultMongo.setImgUrl(SmsApi.YU_MING + "getPhoto?photoId=/" + fileName + ".jpg");    //输入的图片
        personResultMongo.setResult(persion_id); //结果
        personResultMongo.setCreateTime(new Date());  //创建日期
        personResultMongo.setOpenid(openid);  //用户openid
        personResultMongo.setType("看图说话");
        personResultDao.save(personResultMongo);
        return result;
    }
}
